﻿namespace Communication.Help.Crc;

internal static class Crc16Help
{
    internal static byte Reverse(this byte b)
    {
        const uint mask1 = 0x0802u;
        const uint mask2 = 0x22110u;
        const uint mask3 = 0x8020u;
        const uint mask4 = 0x88440u;
        const uint multiplier = 0x10101u;
        const int shift = 16;

        // 分步计算并显式截断
        var part1 = (b * mask1) & mask2;
        var part2 = (b * mask3) & mask4;
        var combined = (part1 | part2) * multiplier;
        return (byte)(combined >> shift);
    }

    internal static ushort Reverse(this ushort value)
    {
        return (ushort)((value >> 8) | (value << 8));
    }
}